﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Ewts.Storage
{
    public partial class VehicleArrange : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataLib.BLL.VehicleArrange bll = new DataLib.BLL.VehicleArrange();
                DataLib.Model.VehicleArrange model = new DataLib.Model.VehicleArrange();
                if (Request["cjoid"] != null && Request["cjoid"].ToString().Trim() != "")
                {
                    model = bll.GetModelBySrcid(Request["cjoid"].ToString().Trim(),1);
                    if(model ==null)
                        bindData(int.Parse(Request["cjoid"].ToString().Trim()));

                }
                else if (Request["outid"] != null && Request["outid"].ToString().Trim() != "")
                {
                    model = bll.GetModelBySrcid(Request["outid"].ToString().Trim(), 0);
                    if (model == null)
                        bindOutData(int.Parse(Request["outid"].ToString().Trim()));
                }
                if (model != null)
                    Response.Redirect("VehicleArrangeModify.aspx?vaid=" + model.id.ToString());
                //收货员绑定 根据处理地派车
                chkVehiclorList.DataSource = new DataLib.BLL.Vehiclor().GetList("city = '" + platecity.Value.Trim() + "'and isdel=0");
                chkVehiclorList.DataTextField = "Name";
                chkVehiclorList.DataValueField = "id";
                chkVehiclorList.DataBind();
                


                //车牌号绑定
                chkPlateNumberList.DataSource = Code.MyCheckBox.GetDataSourceByCity("city = '" + platecity.Value.Trim() + "' and isdeleted=0 and isshow=0"); 
                chkPlateNumberList.DataTextField = "ShortName";
                chkPlateNumberList.DataValueField = "id";
                chkPlateNumberList.DataBind();
                foreach (ListItem li in chkPlateNumberList.Items)
                {
                    li.Attributes.Add("myvalue", li.Value);
                }

            }
        }

        protected void bindData(int cjoid)
        {
            DataLib.BLL.VehicleArrange bll = new DataLib.BLL.VehicleArrange();
            DataTable tb = new DataTable();
            tb = bll.GetList("cjoid=" + cjoid.ToString()).Tables[0];
            if (tb != null && tb.Rows.Count>0)
            {
                string valid = tb.Rows[0]["id"].ToString();
                Response.Redirect("VehicleArrangeModify.aspx?vaid=" +valid );
                return;
            }
            DataLib.Model.CJOItem cjomodel = new DataLib.BLL.CJOItem().GetListByCid(cjoid);
            lblCity.Text = cjomodel.Graphicy;
            platecity.Value = cjomodel.Graphicy;
            lblCjoNo.Text = cjomodel.cjono;
            lblProCode.Text = cjomodel.pronum;
            lblProName.Text = cjomodel.proname;
            lblCustomerName.Text = cjomodel.companyname;
            lblNoticeDate.Text = cjomodel.createtime.ToString();
            lblDateTime.Text = cjomodel.collectdate.ToString();
            lblOadNum.Text = cjomodel.loadnumno;

            DataLib.Model.VehicleRequire mRequire = null;
            try
            {
                mRequire = new DataLib.BLL.VehicleRequire().GetModel(cjomodel.vehichlerequireid);
            }
            catch
            {
            }
            if (mRequire == null)
            {
                return;
            }
            lblOtherRequire.Text = mRequire.otherrequire;
            lblMaternRequire.Text = mRequire.maternrequire;
            lblPeculiarRequire.Text = mRequire.peculiarrequire;
            lblSamplingRequire.Text = mRequire.samplingrequire;
            lblVehiRequire.Text = mRequire.other;
            lblWorkerCount.Text = mRequire.workercount.ToString();
            lblGoodsCount.Text = mRequire.numberestimate.ToString();
            lblTools.Text = mRequire.tools;
           

            DataLib.Model.LoadNum loadM = new DataLib.BLL.LoadNum().GetModel(cjomodel.loadnumid);
            lblRubbInfo.Text = loadM.RubbishType;
            lblRublishArea.Text = loadM.Rubbisharea;
            lblSaleMan.Text = cjomodel.cjoshowmanusername + "- " + cjomodel.cjoshowmantruename;
            lblGoodsWeight.Text = cjomodel.predictweight.ToString();
            lblGoodsInfo.Text = cjomodel.cjoremark;

            DataLib.Model.ProjectsRelation prmodel = new DataLib.BLL.ProjectsRelation().GetModel((int)cjomodel.prorelid);

            lblAddress.Text = cjomodel.cjoaddinfo;

            lblCustomerContactor.Text = cjomodel.cjolinkman;
            lblCustomerPhoneNum.Text = cjomodel.cjophone;
        }
        /// <summary>
        /// 称库派车
        /// </summary>
        /// <param name="outid"></param>
        protected void bindOutData(int outid)
        {

            DataLib.Model.OutStorage outmodel = new DataLib.BLL.OutStorage().GetModel(outid);
            lblCity.Text = outmodel.graphicCity;
            platecity.Value = outmodel.OutDepart;
            lblCjoNo.Text = outmodel.outStorageNo;
            DataLib.BLL.LoadNum bllLoad = new DataLib.BLL.LoadNum();
            lblProleft.Text = "客户编号";
            lblProCode.Text = bllLoad.GetCusm(outmodel);
            lblCustomerName.Text = bllLoad.GetCusm(outmodel);
            //if ((outmodel.inDepart == outmodel.belogarea) && (outmodel.OutDepart != outmodel.graphicCity))
            //{
            //    lblCustomerCode.Text = outmodel.graphicCity + outmodel.OutDepart + "库";
            //    lblCustomerName.Text = outmodel.graphicCity + outmodel.OutDepart + "库";
            //}
            //else
            //{
            //    lblCustomerCode.Text = outmodel.OutDepart + "伟翔";
            //    lblCustomerName.Text = outmodel.OutDepart + "伟翔";
            //}
             lblNoticeDate.Text = "";
            lblDateTime.Text =outmodel.outStorageDate.ToString();
            lblOadNum.Text = outmodel.VLoadNum;

        }


          protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string srcid = "";
            string url = "";
            if (Request["cjoid"] != null && Request["cjoid"].ToString().Trim() != "")
            {
                srcid = Request["cjoid"].ToString().Trim();
                url = CjoVehicle(srcid);
            }
            else if (Request["outid"] != null && Request["outid"].ToString().Trim() != "")
            {
                srcid = Request["outid"].ToString().Trim();
                url = OutVehicle(srcid);
            }

            if (url != "-1")
            {
                DataLib.Common.MessageBox.ShowAndRedirect(this, "派车成功，打印", url);
            }
            
        }

        protected string CjoVehicle(string cjoid)
        {
            string url = "/RDLC/CjoReport.aspx?cjoid=";
            DataLib.Model.VehicleArrange model = new DataLib.Model.VehicleArrange();
            DataLib.BLL.PlateNumber bllplate = new DataLib.BLL.PlateNumber();
            DataLib.BLL.Vehiclor bllveh = new DataLib.BLL.Vehiclor();

            model.cjoid = int.Parse(cjoid);
            model.sealno = hidSealNo.Value;
            model.status = 1;
            model.outid = 0;
            model.platenumberid = Code.MyCheckBox.GetCheckIDColl(chkPlateNumberList, 1);
            model.vehiderid = Code.MyCheckBox.GetCheckIDColl(chkVehiclorList, 0);
            if (model.platenumberid == null || model.platenumberid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一辆车");
                return "-1";
            }
            else 
            {
                model.platenumber = "";
                string[] parr = model.platenumberid.Split(',');
                foreach(string s in parr)
                {
                    if (s != null && s.Trim() != "") 
                    {
                        if (model.platenumber != "")
                            model.platenumber += ",";
                        model.platenumber += bllplate.GetModel(int.Parse(s)).plateno;
                    }
                }

            }
            if (model.vehiderid == null || model.vehiderid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一位收货员");
                return "-1";
            }
            else
            {
                model.vehider = "";
                string[] parr = model.vehiderid.Split(',');
                foreach (string s in parr)
                {
                    if (s != null && s.Trim() != "")
                    {
                        if (model.vehider != "")
                            model.vehider += ",";
                        model.vehider += bllveh.GetModel(int.Parse(s)).name;
                    }
                }
            }
            DataLib.Model.Cjo cmodel = new DataLib.BLL.Cjo().GetModel(int.Parse(cjoid));
            model.cumcode = cmodel.customercode;
            model.cumname = cmodel.customername;
            if (cmodel.istyz == "服务")
            {
                cmodel.status = -3;
            }
            else
            {
                cmodel.status = 2;
            }
            new DataLib.BLL.Cjo().Update(cmodel);
            if (new DataLib.BLL.VehicleArrange().Add(model) > 0)
            {
                url += cjoid;
            }
            else
            {
                url = "-1";
            }
            return url;
        }

        protected string OutVehicle(string outid)
        {
            string url = "/RDLC/CjoReport.aspx?outid=";
            DataLib.Model.VehicleArrange model = new DataLib.Model.VehicleArrange();
            DataLib.BLL.PlateNumber bllplate = new DataLib.BLL.PlateNumber();
            DataLib.BLL.Vehiclor bllveh = new DataLib.BLL.Vehiclor();
            DataLib.BLL.OutStorage bllout = new DataLib.BLL.OutStorage();
            DataLib.BLL.VehicleArrange bll = new DataLib.BLL.VehicleArrange();

            model.outid = int.Parse(outid);
            model.cjoid = 0;
            model.sealno = hidSealNo.Value;
            model.status = 1;
            model.platenumberid = Code.MyCheckBox.GetCheckIDColl(chkPlateNumberList, 1);
            model.vehiderid = Code.MyCheckBox.GetCheckIDColl(chkVehiclorList, 0);
            if (model.platenumberid == null || model.platenumberid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一辆车");
                return "-1";
            }
            else
            {
                model.platenumber = "";
                string[] parr = model.platenumberid.Split(',');
                foreach (string s in parr)
                {
                    if (s != null && s.Trim() != "")
                    {
                        if (model.platenumber != "")
                            model.platenumber += ",";
                        model.platenumber += bllplate.GetModel(int.Parse(s)).plateno;
                    }
                }

            }
            if (model.vehiderid == null || model.vehiderid.Trim() == "")
            {
                DataLib.Common.MessageBox.Show(Page, "请至少选择一位收货员");
                return "-1";
            }
            else
            {
                model.vehider = "";
                string[] parr = model.vehiderid.Split(',');
                foreach (string s in parr)
                {
                    if (s != null && s.Trim() != "")
                    {
                        if (model.vehider != "")
                            model.vehider += ",";
                        model.vehider += bllveh.GetModel(int.Parse(s)).name;
                    }
                }
            } 
            
            DataLib.Model.OutStorage outmodel = bllout.GetModel(int.Parse(outid));
            if(outmodel.VLoadNum.Substring(0,2)=="86")
            {
                DataLib.Model.LoadNum lmodel = new DataLib.BLL.LoadNum().GetLoadID(outmodel.VLoadNum);
                model.cumcode = lmodel.cumcode;
                model.cumname = lmodel.cumname;
            }
            else if (outmodel.inDepart != outmodel.belogarea)
            {
                model.cumcode = "";
                model.cumname = outmodel.belogarea + "伟翔";
            }
            else 
            {
                model.cumcode = "";
                model.cumname = outmodel.belogarea + "伟翔" + outmodel.graphicCity + "库";
            }


          if ((outmodel.inDepart == outmodel.belogarea) 
 //               && (outmodel.OutDepart != outmodel.graphicCity)
                &&(outmodel.StorageType=="非原料库"))
            {
                outmodel.status = 4;
            }
            else
            {
                outmodel.status = 3;
            }
            bllout.Update(outmodel);
            if (bll.Add(model) >= 0)
            {
                url += outid;
            }
            else
            {
                url = "-1";
            }
            return url;
        }
    }
}